﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarPark.Helper;

namespace CarPark.MainModel
{
    public partial class UserAdd : UserControl
    {
        MidCarPark mcp = new MidCarPark();

        public UserAdd()
        {
            InitializeComponent();
            LoadValues();
        }

        /// <summary>
        /// 加载车型数据
        /// </summary>
        public void LoadValues()
        {
            comboBox1.DataSource = null;
            comboBox1.DataSource = mcp.GetAllCarModel();
        }

        /// <summary>
        /// 重置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            foreach (Control cl in this.Controls)
            {
                if (cl is TextBox)
                    cl.Text = string.Empty;
            }
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            this.Hide();
            button2_Click(this, null);
        }

        /// <summary>
        ///  确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (CheckTextBoxEmpty().Count > 0)
                MessageBox.Show("*开头的必填项必须填写！", GlobalConst.GLOBAL_POPUP_TITLE, MessageBoxButtons.OK);
            else
            {
                if (textBox6.Text != textBox7.Text)
                    MessageBox.Show("两次输入的密码不一致！", GlobalConst.GLOBAL_POPUP_TITLE, MessageBoxButtons.OK);
                else
                {
                    string ret;
                    ret = mcp.AddNewUser(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, _textBox5.Text, textBox6.Text, comboBox1.SelectedItem.ToString(), _textBox8.Text);
                    if (ret == GlobalConst.CK_OK)
                        MessageBox.Show("操作成功！", GlobalConst.GLOBAL_POPUP_TITLE, MessageBoxButtons.OK);
                    else
                        MessageBox.Show(string.Format("操作失败！\n错误信息：{0}",ret), GlobalConst.GLOBAL_POPUP_TITLE, MessageBoxButtons.OK);
                }
            }
        }

        /// <summary>
        /// 返回所有没有填写的但是必须填写的文本控件
        /// </summary>
        /// <returns></returns>
        public List<TextBox> CheckTextBoxEmpty()
        {
            List<TextBox> ret = new List<TextBox>();
            foreach (Control cl in this.Controls)
            {
                if (cl is TextBox && !cl.Name.StartsWith("_") && cl.Text == string.Empty)
                {
                    ret.Add(cl as TextBox);
                }
            }
            return ret;
        }


    }


}
